let express = require("express")()
const { request, response, json } = require("express");
let expressStatic = require("express-static")
const port = 8080
var mysql = require('mysql');
var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'news',
    timezone: '08:00'
});

sql.connect()

//查
express.get("/getmessage", (request, response) => {
        sql.query(`SELECT * FROM studentmessage`, (error, data) => {
            if (error) {
                response.send("error")
                console.log(error)
            } else {
                response.send(JSON.stringify(data))
            }
        })
    })
    //删
express.get("/deletemessage", (request, response) => {
        sql.query(`delete from studentmessage where id=${request.query.id}`, (error, data) => {
            if (error) {
                response.send("error")
                console.log(error)
            } else {
                response.send("success")
            }
        })
    })
    //增
express.get("/addmessage", (request, response) => {
        let p = request.query
        sql.query(`insert into studentmessage (name,age,sex,city,joinDate) values ("${p.name}","${p.age}","${p.sex}","${p.city}","${p.joinDate}")`, (error, data) => {
            if (error) {
                response.send("error")
                console.log(error)
            } else {
                response.send("success")
            }
        })
    })
    //改
express.get("/updatemessage", (request, response) => {
    let p = request.query
    sql.query(`update studentmessage set name = "${p.name}",age="${p.age}",sex="${p.sex}",city="${p.city}",joinDate="${p.joinDate}" where id="${p.id}"`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})










express.use(expressStatic(__dirname + '/static'))
express.listen(8080)